package com.sugar.queryinfo.module.business.service.apitypeinfo;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.queryinfo.module.business.enums.apitype.ApiTypeEnums;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.sugar.queryinfo.module.business.controller.admin.apitypeinfo.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.apitypeinfo.ApiTypeInfoDO;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;

import com.sugar.queryinfo.module.business.dal.mysql.apitypeinfo.ApiTypeInfoMapper;

import static com.sugar.queryinfo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.queryinfo.module.business.enums.ErrorCodeConstants.*;
import static com.sugar.queryinfo.module.business.enums.apitype.ApiTypeEnums.TYPE_SF_2E_AUTH;
import static com.sugar.queryinfo.module.business.enums.apitype.ApiTypeEnums.TYPE_TZ_C;

/**
 * API类型 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class ApiTypeInfoServiceImpl implements ApiTypeInfoService {

    @Resource
    private ApiTypeInfoMapper apiTypeInfoMapper;

    @Override
    public Long createApiTypeInfo(ApiTypeInfoSaveReqVO createReqVO) {
        // 插入
        ApiTypeInfoDO apiTypeInfo = BeanUtils.toBean(createReqVO, ApiTypeInfoDO.class);
        apiTypeInfoMapper.insert(apiTypeInfo);

        // 返回
        return apiTypeInfo.getId();
    }

    @Override
    public void updateApiTypeInfo(ApiTypeInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateApiTypeInfoExists(updateReqVO.getId());
        // 更新
        ApiTypeInfoDO updateObj = BeanUtils.toBean(updateReqVO, ApiTypeInfoDO.class);
        apiTypeInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteApiTypeInfo(Long id) {
        // 校验存在
        validateApiTypeInfoExists(id);
        // 删除
        apiTypeInfoMapper.deleteById(id);
    }

    @Override
        public void deleteApiTypeInfoListByIds(List<Long> ids) {
        // 删除
        apiTypeInfoMapper.deleteByIds(ids);
    }


    private void validateApiTypeInfoExists(Long id) {
        if (apiTypeInfoMapper.selectById(id) == null) {
            throw exception(API_TYPE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ApiTypeInfoDO getApiTypeInfo(Long id) {
        return apiTypeInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ApiTypeInfoDO> getApiTypeInfoPage(ApiTypeInfoPageReqVO pageReqVO) {
        return apiTypeInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ApiTypeInfoSimpleRespVO> getApiTypeInfos(Integer type) {
        List<ApiTypeInfoDO> apiTypeInfos = apiTypeInfoMapper.selectList(Wrappers.<ApiTypeInfoDO>lambdaQuery()
                .eq(ApiTypeInfoDO::getStatus, 1).orderByDesc(ApiTypeInfoDO::getSort));

        if (Objects.nonNull(type)) {
            // 只筛选数据查询
            if (type.equals(1)){
                return BeanUtils.toBean(apiTypeInfos.stream().filter(ApiTypeInfoDO::getShowDataQuery).collect(Collectors.toList()), ApiTypeInfoSimpleRespVO.class);
            }else if (type.equals(2)){ // 只筛选智能审核的
                return BeanUtils.toBean(apiTypeInfos.stream().filter(ApiTypeInfoDO::getShowAudit).collect(Collectors.toList()), ApiTypeInfoSimpleRespVO.class);
            }
        }

        return BeanUtils.toBean(apiTypeInfos, ApiTypeInfoSimpleRespVO.class);
    }

    @Override
    public ApiTypeInfoDO getApiPriceByType(ApiTypeEnums apiTypeEnums) {
        return getApiPriceByType(apiTypeEnums.getApiEnum().getApiKey());
    }

    @Override
    public ApiTypeInfoDO getApiPriceByType(String code) {
        ApiTypeInfoDO apiTypeInfoDO = apiTypeInfoMapper.selectOne(ApiTypeInfoDO::getCode, code);

        if (Objects.isNull(apiTypeInfoDO)) {
            throw exception(API_TYPE_INFO_NOT_EXISTS);
        }

        return apiTypeInfoDO;
    }

    @Override
    public BigDecimal getApiZnshPrice() {
        // 获取 二要素校验价格
        ApiTypeInfoDO auth2 = apiTypeInfoMapper.selectOne(ApiTypeInfoDO::getCode, TYPE_SF_2E_AUTH.getApiEnum().getApiKey());
        // 获取 探针C价格
        ApiTypeInfoDO tzC = apiTypeInfoMapper.selectOne(ApiTypeInfoDO::getCode, TYPE_TZ_C.getApiEnum().getApiKey());
        return auth2.getPrice().add(tzC.getPrice());
    }

    @Override
    public void updateApiTypeInfoStatus(ApiTypeInfoUpdateStatusReqVO updateStatusReqVO) {
        Long id = updateStatusReqVO.getId();
        Boolean status = updateStatusReqVO.getStatus();
        Boolean showAudit = updateStatusReqVO.getShowAudit();
        Boolean showDataQuery = updateStatusReqVO.getShowDataQuery();
        if (status != null || showAudit != null || showDataQuery != null){
            apiTypeInfoMapper.update(Wrappers.<ApiTypeInfoDO>lambdaUpdate()
                    .eq(ApiTypeInfoDO::getId, id)
                    .set(status != null, ApiTypeInfoDO::getStatus, status)
                    .set(showAudit != null, ApiTypeInfoDO::getShowAudit, showAudit)
                    .set(showDataQuery != null, ApiTypeInfoDO::getShowDataQuery, showDataQuery));
        }
    }

}